package cn.chuxing.pl.common.base;

import com.github.pagehelper.PageHelper;
import org.apache.ibatis.annotations.Param;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * 实现BaseService抽象类
 * Created by Cooper on 2017/01/07.
 */
public abstract class BaseServiceImpl<Record, Example> implements BaseService<Record, Example> {

	protected BaseMapper<Record, Example> mapper;

	@Override
	public int countByExample(Example example) {
		return mapper.countByExample(example);
	}

	@Override
	public int deleteByExample(Example example) {
		return mapper.deleteByExample(example);
	}

	@Override
	public int deleteByPrimaryKey(Integer id) {
		return mapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(Record record) {
		return mapper.insert(record);
	}

	@Override
	public int insertSelective(Record record) {
		return mapper.insertSelective(record);
	}

	@Override
	public List<Record> selectByExample(Example example) {
		return mapper.selectByExample(example);
	}

	@Override
	public List<Record> selectByExampleForStartPage(Example example, Integer pageNum, Integer pageSize) {
		PageHelper.startPage(pageNum, pageSize, false);
		return mapper.selectByExample(example);
	}

	@Override
	public List<Record> selectByExampleForOffsetPage(Example example, Integer offset, Integer limit) {
		PageHelper.offsetPage(offset, limit, false);
		return mapper.selectByExample(example);
	}

	@Override
	public Record selectFirstByExample(Example example) {
		List<Record> result = selectByExample(example);
		if (null != result && result.size() > 0) {
			return result.get(0);
		}
		return null;
	}

	@Override
	public Record selectByPrimaryKey(Integer id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByExampleSelective(@Param("record") Record record, @Param("example") Example example) {
		return mapper.updateByExampleSelective(record,example);
	}

	@Override
	public int updateByExample(@Param("record") Record record, @Param("example") Example example) {
		return mapper.updateByExample(record,example);
	}

	@Override
	public int updateByPrimaryKeySelective(Record record) {
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Record record) {
		return mapper.updateByPrimaryKey(record);
	}

	@PostConstruct
	public void initMapper() {
		this.mapper = getMapper();
	}

	public abstract BaseMapper<Record,Example> getMapper();
}